package net.slteam.base;

import com.google.common.base.CaseFormat;
import net.slteam.base.util.Convert;
import net.slteam.base.util.ServletUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Objects;
import java.util.logging.Logger;

/**
 * 分页数据
 *
 * @author ruoyi
 */

public class QPage implements Serializable, IPage
{

    static Logger log = Logger.getLogger(QPage.class.getName());
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "pageNum";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String ORDER_IS_ASC = "isAsc";


    public static final int DEFAULT_PAGE_NUM = 1;

    public static final int DEFAULT_PAGE_SIZE = 10;

    enum Order {
        ASC,DESC
    }

    public static final Order DEFAULT_ORDER_BY = Order.ASC;



    /** 当前记录起始索引 */
    private Integer pageNum;

    /** 每页显示记录数 */
    private Integer pageSize;

    /** 排序列 */
    private String orderByColumn;

    /** 排序的方向desc或者asc */
    private String isAsc = DEFAULT_ORDER_BY.name().toLowerCase();

    public String getOrderBy()
    {
        if (StringUtils.isEmpty(orderByColumn))
        {
            return "";
        }
        
        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, orderByColumn) + " " + isAsc;
    }

    @Override
    public Integer getPageNum()
    {
        return pageNum;
    }

    @Override
    public void setPageNum(Integer pageNum)
    {
        this.pageNum = pageNum;
    }

    @Override
    public Integer getPageSize()
    {
        return pageSize;
    }

    @Override
    public void setPageSize(Integer pageSize)
    {
        this.pageSize = pageSize;
    }

    public String getOrderByColumn()
    {
        return orderByColumn;
    }

    public void setOrderByColumn(String orderByColumn)
    {
        this.orderByColumn = orderByColumn;
    }

    public String getIsAsc()
    {
        return isAsc;
    }

    public void setIsAsc(String isAsc)
    {
        this.isAsc = isAsc;
    }

    public static QPage fromServlet(HttpServletRequest request) {
        if (!request.getParameterMap().containsKey(PAGE_NUM) && !request.getParameterMap().containsKey(PAGE_SIZE)) {
            return null;
        }

        QPage QPage = new QPage();
        QPage.setPageNum(Convert.toInt(request.getParameter(PAGE_NUM), DEFAULT_PAGE_NUM));
        QPage.setPageSize(Convert.toInt(request.getParameter(PAGE_SIZE), DEFAULT_PAGE_SIZE));
        QPage.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));

        Order order = DEFAULT_ORDER_BY;
        String isAsc = request.getParameter(ORDER_IS_ASC);
        if (!StringUtils.isBlank(isAsc)) {
            try {
                order = Order.valueOf(isAsc.trim().toUpperCase());
                QPage.setIsAsc(order.name().toLowerCase());
            } catch (Exception e) {
            }
        }
        QPage.setIsAsc(order.name().toLowerCase());

        return QPage;
    }

    @Override
    public String toString() {
        return "PageDomain{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                ", orderByColumn='" + orderByColumn + '\'' +
                ", isAsc='" + isAsc + '\'' +
                '}';
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof QPage)) return false;
        QPage that = (QPage) o;
        return Objects.equals(pageNum, that.pageNum) && Objects.equals(pageSize, that.pageSize) && Objects.equals(
                orderByColumn, that.orderByColumn) && Objects.equals(isAsc, that.isAsc);
    }

    @Override
    public int hashCode() {
        return Objects.hash(pageNum, pageSize, orderByColumn, isAsc);
    }
}
